//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<XFrame.cpp>
///	@path	~/src/gui/xgui/
///	@date	2008/09/25
///	@brief	.

#define _FULLAPI
#include "config/config.h"

#include "graphics/IRenderer.h"

#include "gui/composite/composition.h"
#include "gui/composite/CCompositeTexture.h"
#include "gui/xgui/XFrame.h"
#include "gui/xgui/XDesktop.h"

namespace xeres {

	IMPL_IOBJECT_CLASS_DYNAMIC( XFrame );

	// XFrame
	XFrame::XFrame( void )
		: XPanel()
	{
	}
	// XFrame
	XFrame::XFrame( const Rect<int>& placement )
		: XPanel( placement )
	{
	}

	// XFrame
	XFrame::XFrame( const Pair<int>& size )
		: XPanel( size )
	{
	}

	// ~XFrame
	XFrame::~XFrame( void )
	{
	}

	// SetPosition
	bool XFrame::SetPosition( const Pair<int>& pos )
	{
		// we override this section to skip update call.
		RefWeak<XDesktop> desktop = GetParent();
		if( desktop.IsValid() )
			desktop->OnChildRepose( this );

		m_position = pos;

		// Notify pos change.
		OnPos();

		if( desktop.IsValid() )
			desktop->OnChildRepose( this );
		return true;
	}

	// SetSize
	bool XFrame::SetSize( const Pair<int>& size )
	{
		if( !__super::SetSize( size ) )
			return false;
		// FIXME: should we do some process?
		return true;
	}

	// SetParent
	bool XFrame::SetParent( RefWeak<IGuiForm> parent )
	{
		if( !parent->IsA<XDesktop>() )
		{
			TRACE_ERROR( _S("XFrame::SetParent: XFrame can only be a child of XDesktop, failed to set '%s' as parent.") ,
				parent->GetClassName().c_str() );
			return false;
		}
		return __super::SetParent( parent );
	}

	// OnClose
	void XFrame::OnClose( void )
	{
		__super::OnClose();
	}

	// RequestUpdate
	void XFrame::RequestUpdate( RefWeak<IGuiComp> comp , const Rect<int>& region )
	{
		Rect<int> r( region );
		if( comp != this )
		{
			XS_ASSERT( comp->GetParent() == this );
			if( !comp->GetVisible() )
				return;

			r.offset( comp->GetPosition() );
			r = r & GetRect();

			if( r.is_empty() )
				return;
		}

		if( m_updateRect.is_empty() )
			m_updateRect = r;
		else
			m_updateRect.union_rect( r );
	}

	// Tick
	void XFrame::Tick( Updater& updater , float dTime )
	{
		__super::Tick( updater , dTime );
	}

	// Draw
	void XFrame::Draw( Composition& composition )
	{
		__super::Draw( composition );
	}

	// SetFrameEdge
	void XFrame::SetFrameEdge( const Pair<int>& edge )
	{
	}

	// GetFrameEdge
	const Pair<int>& XFrame::GetFrameEdge( void )
	{
		static const Pair<int> s_zero( 0 , 0 );
		return s_zero;
	}

} // namespace xeres
